<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

	<%-- CSS Style Declarations --%>
	<link rel ="stylesheet" type="text/css" href="stylesheets/style.css" ></link>
	
	<%-- Java imports --%>
	<%@ page import="java.io.*" %>
	<%@ page import="java.util.*" %>
	<%@ page import="flashcardtest.FlashCardSet" %>
	<%@ page import="flashcardtest.FlashCard" %>
	<%@ page import="flashcardtest.FlashCardCore" %>
	<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
	<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
	<%@ page import="com.google.appengine.api.datastore.Entity" %>
	<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
	<%@ page import="com.google.appengine.api.datastore.Query" %>
	<%@ page import="com.google.appengine.api.users.UserService" %>
	<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

	<%-- Get reference to Java Objects --%>
	<% FlashCardCore core = (FlashCardCore) request.getSession().getAttribute("core");
		if (core == null) {
			core = new FlashCardCore();
			request.getSession().setAttribute("core", core);
		}
		%>
	
	<%-- Preprocessing --%>
	<% 		
		UserService userService = UserServiceFactory.getUserService();
		String thisURL = request.getRequestURI();
		response.setContentType("text/html") ;
		if (request.getUserPrincipal() != null) {
			//core.setUserName(request.getUserPrincipal().getName());
			response.sendRedirect("/");
		}		
	 %>

<title>Login</title>
</head>
<body>

<div id="center-wrapper">
<div id="center-column">

<!-- Header -->
<div id="header">
</div> <!-- header -->

<!-- Login Link -->
<div style="
	position:absolute;
	top:100px;
	width:100%;
	color:#FFFFFF;
	font-weight:bold;	
	text-align:center;">

<%		userService = UserServiceFactory.getUserService();
		thisURL = request.getRequestURI();
		response.setContentType("text/html") ;
		if (request.getUserPrincipal() != null) {
			//response.getWriter().println("<p>Hello, " + 
			out.println("<p>Hello, " +
					request.getUserPrincipal().getName() + 
					"! You can <a href=\"" + 
					userService.createLogoutURL(thisURL) + 
					"\">sign out</a>.<p>");
		}
		else {
			//response.getWriter().println("<p>Please <a href=\"" + 
			out.println("<p>Please <a href=\"" +
					userService.createLoginURL(thisURL) +
					"\">sign in</a>.<p>");
		}
	%>	
</div> <!-- login link -->
</div> <!-- center-column -->
</div> <!-- center-wrapper -->

</body>
</html>